#include "stdafx.h"
#include "ViewManager.h"

using namespace mcore;

ViewManager * mcore::gViewManager = NULL;

ViewManager::ViewManager()
{
	setActiveView(new View);
	gViewManager = this;
}

ViewManager::~ViewManager()
{
	gViewManager = NULL;
	for(ViewMap::iterator it = mViews.begin() ; it != mViews.end() ; ++it)
	{
		it->second->unload();
		delete it->second;
	}
}

void ViewManager::addView(View * view)
{
	assert(view != NULL);
	assert(mViews.find(view->getName()) == mViews.end());

	mViews[view->getName()] = view;
}

void ViewManager::setActiveView(View * view)
{
	assert(view != NULL);

	if(mViews.find(view->getName()) == mViews.end())
		mViews[view->getName()] = view;

	mActiveView = view;

	if(!mActiveView->isLoaded())
		mActiveView->load();
}

bool ViewManager::setActiveView(const std::string & name)
{
	if(mViews.find(name) == mViews.end())
		return false;
	else
	{
		mActiveView = mViews[name];

		if(mActiveView->isLoaded())
			mActiveView->load();

		return true;
	}
}

void ViewManager::onResize()
{
	mActiveView->onResize();
}

void ViewManager::update(float dt)
{
	mActiveView->update(dt);
}

void ViewManager::draw()
{
	mActiveView->draw();
}

bool ViewManager::onWindowMessage(UINT msg, WPARAM wParam, LPARAM lParam)
{
	return mActiveView->onWindowMessage(msg, wParam, lParam);
}